//以下为修改jQuery Validation插件兼容Bootstrap的方法，没有直接写在插件中是为了便于插件升级
$.validator.setDefaults({
	highlight: function (element) {
		$(element).closest('.form-group').removeClass('has-success').addClass('has-error');
    },success: function (element) {
    	element.closest('.form-group').removeClass('has-error').addClass('has-success');
    },errorElement: "span",
    errorPlacement: function (error, element) {
        if (element.is(":radio") || element.is(":checkbox")) {
            error.appendTo(element.parent().parent().parent());
        } else {
            error.appendTo(element.parent());
        }
    },errorClass: "help-block m-b-none",
    validClass: "help-block m-b-none"
});
jQuery.validator.addMethod("isMobile", function(value, element) {
    var length = value.length;
    var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
    return this.optional(element) || (length == 11 && mobile.test(value));
}, "请正确填写您的手机号码");

$().ready(function () {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#registerForm").validate({
		onfocusout: function(element) { $(element).valid();},
		errorPlacement: function (error, element) {
			if (element.is(":radio") || element.is(":checkbox")) {
	            error.appendTo(element.parent().parent().parent());
	        }else if (element.is("#checkNum")) {
	            error.appendTo(element.parent().parent());
	        } 
			else {
	            error.appendTo(element.parent());
	        }
	    },
        rules: {
        	username: {
        		required:true,
        		minlength:2,
        		remote:{
        			type:"post",
        			url:"../admin/check/username",
        			data:{
        				"username":function(){return  $("#username").val();}
        			},dataFilter: function (data) {
                 		data=eval("("+data+")");
                 		return data.success;
                 	}
        		}
        	},
        	name: {
        		minlength:2,
        		required: true 
        	},
        	phone: {
        		required:true,
        		isMobile:true,
        		minlength:11,
        		remote:{
        			type:"post",
        			url:"../admin/check/phone",
        			data:{
        				"phone":function(){return  $("#phone").val();}
        			},dataFilter: function (data) {
                 		data=eval("("+data+")");
                 		return data.success;
                 	}
        		}
        	},
        	checkNum: {
        		 digits : true,
                 required : true,
                 remote:{
         			type:"post",
                 	url:"../admin/check/code",
                 	data : {
                 		"phone": function() {return $("#phone").val();},
                 		"code": function() {return $("#checkNum").val();}
                 	},dataFilter: function (data) {
                 		data=eval("("+data+")");
                 		return data.success;
                 	}
         		}
        	},
        	password: {
        		 required : true,
        		 minlength: 5
        	},
        	repassword:{
        		required: true,
                minlength: 5,
                equalTo: "#password"
        	}
        },
        messages: {
        	username: {
        		required:icon +"用户名不能为空",
        		minlength:icon +"用户名长度不能小于2",
        		remote:icon + "用户名已存在"
        	},
        	name: {
        		minlength:icon +"真实姓名长度不能小于2",
        		required: icon +"真实姓名不能为空",
        	},
        	phone: {
        		required:icon + "用户手机号不能为空",
        		isMobile:icon + "请输入正确的手机号",
        		minlength:icon + "手机号长度不正确",
        		remote:icon + "该手机号已注册"
        	},
        	checkNum: {
        		 digits : icon + "验证码只能为数字",
                 required : icon + "验证码不能为空",
                 remote:icon+"验证码不正确"
        	},
        	password: {
        		 required : icon + "密码不能为空",
        		 minlength: icon + "密码不能小于5位"
        	},
        	repassword:{
        		required: icon + "确认密码不能为空",
                minlength: icon + "确认密码不能小于5位",
                equalTo: icon + "两次密码不一致"
        	}
        }
    });
	$("#checkNumBtn").on("click",function(){
		if($("#registerForm").validate().element($("#phone"))){
			$.ajax({
				url:"../sms/send",
				type:"get",
				dataType:"json",
				data:{phone:$("#phone").val()},
				success : function(data){
					if(data.success){
						var time = 10;
						setInterval(function(){
			                if(time<=0){
			                	$("#checkNumBtn").html("重新获取").removeAttr("disabled").css("color","")
			                    return;
			                }
			                time--;
			                $("#checkNumBtn").html(time+"秒后重新获取").attr("disabled","disabled").css("color","#fff");
			            },1000);
						
					}else{
						
					}
				},error:function(a,b,c){
					swal({
                        title: "获取失败",
                        text: "系统异常！请稍后再试",
                        type: "error"
                    });
				}
			});
		}
	});
	$("#registerBtn").on("click",function(){
		if($("#registerForm").validate().form()){
			$.ajax({
				url :"../admin/register",
				type : "post",
				dataType:"json",
				data : $('#registerForm').serialize(),
				success : function(data){
					if(data.success){
						swal({
					        title: "注册成功",
					        type: "success"
					    },function(){
					    	window.location.href="../admin/indexpage";	
					    });
					}else{
						swal({
	                        title: "注册失败",
	                        text: "系统异常！请稍后再试",
	                        type: "warning"
	                    });
						
					}
				},error:function(a,b,c){
					swal({
                        title: "注册失败",
                        text: "系统异常！请稍后再试",
                        type: "warning"
                    });
				}
			});
		}
	});
});
